using System.Windows;
using System.Windows.Controls;
using TwitterLib;
using System;
using System.ComponentModel;

namespace Pawing.Controls.Search
{
    public partial class TweetHunt : UserControl
    {
        public TweetHunt()
        {
            InitializeComponent();
            _model = new TweetHuntViewModel();
        }

        private void OnLoad(object sender, RoutedEventArgs e)
        {
            DataContext = _model;
            _model.CurrentType = SearchType.Status;
        }

        private void OnAddSearchItem(object sender, System.Windows.RoutedEventArgs e)
        {
            _model.AddCurrentWordAsSearchTerm();
        }

        private void OnDeleteSearchItem(object sender, System.Windows.RoutedEventArgs e)
        {
            var element = sender as FrameworkElement;
            if (element != null)
            {
                _model.DeleteTerm(element.DataContext as SearchItem);
            }
        }

        private TweetHuntViewModel _model;

        #region
        public static readonly RoutedEvent NameClickedInTweetHuntEvent = EventManager.RegisterRoutedEvent(
            "NameClickedInTweetHunt", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TweetHunt));

        public event RoutedEventHandler NameClickedInTweetHunt
        {
            add { AddHandler(NameClickedInTweetHuntEvent, value); }
            remove { RemoveHandler(NameClickedInTweetHuntEvent, value); }
        }

        private void NameClickedInTweet(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(NameClickedInTweetHuntEvent, e.OriginalSource));
        }

        public static readonly RoutedEvent HashtagClickedInTweetHuntEvent = EventManager.RegisterRoutedEvent(
            "HashtagClickedInTweetHunt", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TweetHunt));

        public event RoutedEventHandler HashtagClickedInTweetHunt
        {
            add { AddHandler(HashtagClickedInTweetHuntEvent, value); }
            remove { RemoveHandler(HashtagClickedInTweetHuntEvent, value); }
        }

        private void HashtagClickedInTweet(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(HashtagClickedInTweetHuntEvent, e.OriginalSource));
        }
        #endregion

        private void TextBlock_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var element = sender as FrameworkElement;
            SearchItem searchItem = element.DataContext as SearchItem;
            _model.RefreshSearchItem(searchItem);
            _model.SearchResults.RemoveResultsAssociatedwithItem(searchItem);
            //if (element != null)
            //{
            //    _model.SearchResults.KeepResultsAssociatedwithItem(searchItem);
            //}
        }

        private void TypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_model != null)
            {
                if (TypeComboBox.SelectedIndex == 0)
                    _model.CurrentType = SearchType.Status;
                else
                    _model.CurrentType = SearchType.User;
            }
        }

        private void ListBox_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            RaiseEvent(e);
        }
    }
}
